In this demonstration, you will learn
synchronous communication between different services in continuation to the last demo. Now create the router module. First, you need the
express module. Create the router. Import the controller
layer and the model layer. Create a router post
method that will take the parameters as
root and the call back that is async in nature with request and
response objects. Add the try and catch blocks. Inside the try block, create the customer
object by passing request that body as
constructor parameter. The customer object will have all the data coming
from the request body. Rather than writing
single parameters of the request body, write three dots with
the request body. Now call the controller layer. The saveCustomer
method will pass the newCustomer object and the callback having
the error in result. If the controller
returns the error, then the status would be 400. The message will be an error
while saving the customer. Print that error object as well. Inside the else block, specify response.status
as success, that is 200. Send the message as a result. Inside the catch block, add response.status as 500, with the message as
internal server error. Similarly, you will have
another router.get method, which will take the
first parameter as ID and the second parameter
as async call back. Retrieve the customerId from the parameters of
the request object. Call the getCustomerById of the controller and pass
the customer object, customerId and the callback. If the controller
returns the error, then the status will be 400 with the message, error
getting customer. If the controller
returns the object, then the status will be 200
and will print the object. Inside the catch block, send the error or write the
message sum internal error. Lastly, export the module. Here you are done with
the router layer as well. Now go to the index.js file. First, import the
express module, create the app and import
the customer router. Use the express.json
middle ware. Create a middleware for
the customer router again and use the port 4, 000. You also need to do a few installations such as the express module and mongoose. To install mongoose, write npm i mongoose. Since mongoose is already
installed in my system, hence not running it. Now run the service. Right now you are inside
the folder Microservices. You have to go inside the
customer microservices. So write cd Customer
with node index.js. The screen shows
listening and connection. Make sure that you have MongoDB
installed in your system. You do not have to
create any database. The database will be created as per the information
given in the file. But you have to install MongoDB. The screen shows
that the port is ready and the port
disconnected to the database. Now test the service in postman. Inside the postman, the URL is localhost port
4, 000 and customer. As in the index file, you have the customer object, and through the router, the file will take the slash. You have to pass the customerId, name, address, and more. Add customerId as 101, a random customer name, the age, and the
address as Florida. Send the message. You get success 200 with the message being
saved in the database. The message generates
an ID also. In the next demo, you will learn how to
create the product and the order class. Thank you.